/*
 * CS_SLOT_PLAYER_CREATE.cpp
 *
 *  Created on: 2013. 9. 7.
 *      Author: jjaehuny
 */

#include "../../engine.h"
#include "../../User.h"
#include "../../Proxy.h"

#include "world.h"

#include "../../NResource/Static.h"
#include "../../NLogic/VirtualCurrency.h"

namespace ProjectZ { namespace NCommand { namespace NWorld {

void CS_SLOT_PLAYER_CREATE::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * // 슬롯 데이터 생성 요청
	CS_SLOT_PLAYER_CREATE	0x0917
	{
		u8	slot_idx;
		u8	class_type;
		u8	u8useDefaultStat;	// 0:최근 사용한 랜덤스탯 사용, 1:디폴트스탯 사용
	}

	// 슬롯 아이디 컨펌
	SC_SLOT_PLAYER_CREATE	0x0918
	{
		s16 ack;			/// 1:생성 완료 , -2 동일한 아이디 존재, -1 생성실패
	}
	 */

	STRESSLOG("[CHANNEL] CS_SLOT_PLAYER_CREATE::ONEXECUTE");

	PREPARE_PACKET;

	uint8_t slot_number = 0;
	uint8_t class_type = 0;
	uint8_t defaultstat = 0;
	* pPacket >> slot_number >> class_type >> defaultstat;

	if (slot_number < 0 || slot_number >= 8)
	{
		DEBUGLOG("CS_SLOT_PLAYER_CREATE slot_number : %d", slot_number);
		return;
	}

	if (pUser->GetUserInfo().array_Slot[slot_number].character_seq > 0 && true == pUser->GetUserInfo().array_Slot[slot_number].makeCharacter)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_SLOT_PLAYER_CREATE);
		pUser->Send(pAck);
		return;
	}

	int ret = ACK_OK;

	int str = 0;
	int con = 0;
	int dex = 0;
	int spi = 0;

	if( ret == 1 )
	{
		if( defaultstat == true )
		{
			str = NLogic::NFunction::GetDefaultStat(class_type, STAT_STR);
			con = NLogic::NFunction::GetDefaultStat(class_type, STAT_CON);
			dex = NLogic::NFunction::GetDefaultStat(class_type, STAT_DEX);
			spi = NLogic::NFunction::GetDefaultStat(class_type, STAT_SPI);
		}
		else
		{
			str = rand() % (STAT_MAX-STAT_MIN+1) + STAT_MIN;
			dex = rand() % (STAT_MAX-STAT_MIN+1) + STAT_MIN;
			spi = rand() % (STAT_MAX-STAT_MIN+1) + STAT_MIN;
			con = STAT_SUM_MAX - (str + dex + spi);
		}

		int character_seq = 0;
		{
			::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
			clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
			clsAuto->MakeQuery("call sp_insert_info_character(%d, %d, %d, %d, %d, %d, %d)", pUser->GetUserSeq(), slot_number,
					class_type, str, dex, con, spi);
			DEBUGLOG("CS_SLOT_PLAYER_CREATE query : %s", clsAuto->GetQueryString());
			if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
			{
				DEBUGLOG("CREATECHARFAIL false == ::Database::GETSESSION()->SyncExecute(1, clsAuto)");
				return;
			}

			if( clsAuto->GetErrorIdx() != 0 )
			{
				DEBUGLOG("CREATECHARFAIL CS_SLOT_PLAYER_CREATE clsAuto->GetErrorIdx() != 0");
				return;
			}

			if (0 == clsAuto->ResultRowCount())
			{
				DEBUGLOG("CREATECHARFAIL CS_SLOT_PLAYER_CREATE 0 == clsAuto->ResultRowCount()");
				return;
			}

			while( clsAuto->Next() )
			{
				clsAuto->GetRecord() >> character_seq;
			}
		}

		if (pUser->GetUserInfo().main_slot_index == -1)
		{
			pUser->GetUserInfo().main_slot_index = slot_number;

			pUser->GetUserInfo().array_Slot[slot_number].open = true;
			pUser->GetUserInfo().array_Slot[slot_number].character_seq = character_seq;
			pUser->GetUserInfo().array_Slot[slot_number].makeCharacter = true;
			pUser->GetUserInfo().array_Slot[slot_number].level	= 1;
			pUser->GetUserInfo().array_Slot[slot_number].classtype = class_type;

			//pUser->GetCharacterInfo().seq = character_seq;
			pUser->GetCharacterInfoFromIndex(slot_number).characterseq = character_seq;

			// 이벤트 던전 스태미너 (최초 캐릭터 생성시 -1로 설정)
			pUser->GetCharacterInfoFromIndex(slot_number).event_stamina	= -1;

			if (pUser->GetUserInfo().array_Slot[1].open == false)
			{
				pUser->GetUserInfo().array_Slot[1].open = true;
				pUser->GetUserInfo().array_Slot[1].character_seq = 0;
				pUser->GetUserInfo().array_Slot[1].makeCharacter = false;
				pUser->GetUserInfo().array_Slot[1].remainStatResetCount = 3;
			}

//			for(int i=0; i < MAX_DUNGEON_COUNT; i++)
//			{
//				pUser->Get_array_dungeon_clear_point()[i] = -1;
//			}
			pUser->GetDungeonClearPoint().Initialize(-1);

			::ProjectZ::NProxy::FlushUserSlotInfoSyn(pUser, slot_number);
		}
		else
		{
			pUser->GetUserInfo().array_Slot[slot_number].open = true;
			pUser->GetUserInfo().array_Slot[slot_number].character_seq = character_seq;
			pUser->GetUserInfo().array_Slot[slot_number].makeCharacter = true;
			pUser->GetUserInfo().array_Slot[slot_number].level	= 1;
			pUser->GetUserInfo().array_Slot[slot_number].classtype = class_type;

			pUser->GetCharacterInfoFromIndex(slot_number).characterseq = character_seq;

			// 이벤트 던전 스태미너 (최초 캐릭터 생성시 -1로 설정)
			pUser->GetCharacterInfoFromIndex(slot_number).event_stamina	= -1;

//			for(int i=0; i < MAX_DUNGEON_COUNT; i++)
//			{
//				pUser->Get_array_dungeon_clear_point()[i] = -1;
//			}
			pUser->GetDungeonClearPoint().Initialize(-1);

			::ProjectZ::NProxy::FlushUserSlotInfoSyn(pUser, slot_number);
		}
	}

}


} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/
